﻿///<reference path="/Scripts/JQuery/jquery-1.9.1-vsdoc.js" />
///<reference path="/ScriptsG/DynaContentDynamic.js" />

/*#region Default values*/
var defaultDialogWidth = 800;
var defaultDialogHeight = 600;
var defaultSmallDialogWidth = 450;
var defaultSmallDialogHeight = 200;
/*#endregion*/

/*#region Core common*/

$.fn.MainAjaxFailure = function (a, b, c) {
	/// <summary>
	/// [INNOVACALL Framework specific] Method called when an MVC.ajax form post fails
	/// </summary>
	/// <param name="a"></param>
	/// <param name="b"></param>
	/// <param name="c"></param>
    if (a.scriptToExecute != null) {
        if (a.scriptToExecute != "") {
            eval(a.scriptToExecute);
        }
    } else {
        $.fn.jqDialogAjaxError(b + " " + a.status + ":" + a.statusText, a.responseText, "", 600, 400);
    }
}

$.fn.serializeObject = function () {
    /// <summary>
    /// Serialize all controls of a form
    /// </summary>
    /// <returns type="object">the object</returns>
    var obj = {};
    $(":input[type='checkbox']").each(function (e) {
        obj[this.name] = this.checked;
    });
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (obj !== undefined) {
                if (obj[this.name] !== undefined) {
                    o[this.name] = obj[this.name];
                }
            }
            else {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            }
        }
        else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};


$.fn.NavigateTo = function (url) {
	/// <summary>
	/// Navigate to URL
	/// </summary>
	/// <param name="url"></param>
    window.location.href = url;
}

$.fn.ensureStopPropagation = function (evt) {
	/// <summary>
    /// Ensure user action stop immediately
	/// Example : If you have a clickable element inside an other clickable element
	/// </summary>
	/// <param name="evt"></param>
    evt.preventDefault();
    evt.stopPropagation();
    evt.stopImmediatePropagation();
}
/*#endregion*/

/*#region Unobstrusive Validation fixup*/
$.validator.methods.range = function (value, element, param) {
    var globalizedValue = value.replace(",", ".");
    return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
}
$.validator.methods.number = function (value, element) {
    return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}
/*#endregion*/

/*#region client components specific*/
$.fn.kendoMenuOpenFixup = function (e) {
    ///<summary>
    ///A fix for Kendo UI Menu
    ///</summary>
    var evt = e;
}

$.fn.PrepareLayoutMenus = function () {
    ///<summary>
    ///Prepare drop down menus
    ///</summary>
    $("#layoutMenu").kendoMenu({
        open: $.fn.kendoMenuOpenFixup
    });
    $("#layoutMenu").removeClass("notDisplayed");
}

jQuery(function ($) {
	/// <summary>
	/// French localization for datepicker
	/// </summary>
    if ($.datepicker != null) {
        $.datepicker.regional['fr'] = { clearText: 'Effacer', clearStatus: '',
            closeText: 'Fermer', closeStatus: 'Fermer sans modifier',
            prevText: '<Préc', prevStatus: 'Voir le mois précédent',
            nextText: 'Suiv>', nextStatus: 'Voir le mois suivant',
            currentText: 'Courant', currentStatus: 'Voir le mois courant',
            monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin',
		'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
            monthNamesShort: ['Jan', 'Fév', 'Mar', 'Avr', 'Mai', 'Jun',
		'Jul', 'Aoû', 'Sep', 'Oct', 'Nov', 'Déc'],
            monthStatus: 'Voir un autre mois', yearStatus: 'Voir un autre année',
            weekHeader: 'Sm', weekStatus: '',
            dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
            dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
            dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
            dayStatus: 'Utiliser DD comme premier jour de la semaine', dateStatus: 'Choisir le DD, MM d',
            dateFormat: 'dd/mm/yy', firstDay: 0,
            initStatus: 'Choisir la date', isRTL: false
        };
        $.datepicker.setDefaults($.datepicker.regional['fr']);
    }
});

/*#endregion*/

/*#region Dialog boxes*/
$.fn.inlinePopup = function (url, title, width, height) {
    ///<summary>
    ///Create a popup window with a JQuery UI Dialog with a frame inside
    ///</summary>
    ///<param name="url" type="String">
    /// IFrame source URL
    ///</param>
    ///<param name="title" type="String">
    /// Title of the dialog
    ///</param>
    ///<param name="width" type="int">
    /// Width in pixels of the dialog box (default : defaultDialogWidth)
    ///</param>
    ///<param name="height" type="int">
    /// Height in pixels of the dialog box (default : defaultDialogHeight)
    ///</param>
    if (width == null) { width = defaultDialogWidth; }
    if (height == null) { height = defaultDialogHeight; }
    var IP = $("#inlinePopup");
    var IFR = $("#ifrInlinePopup");
    IFR.attr("src", url);
    IP.attr("title", title);
    IP.dialog({
        height: height,
        width: width,
        modal: true,
        resizable: false,
        closeOnEscape: true
    });
}

$.fn.jqDialog = function (idDiv, title, redirection, width, height) {
    ///<summary>
    /// Create a simple JQueryUI Dialog box
    ///</summary>
    ///<param name="idDiv" type="String">
    /// The id of the div to be transformed to a dialog box
    ///</param>
    ///<param name="title" type="String">
    /// The title of the dialog box
    ///</param>
    ///<param name="redirection" type="String">
    /// if this parameter is set ok buton navigate to redirection address
    ///</param>
    ///<param name="width" type="int">
    /// Width in pixels of the dialog box (default : defaultDialogWidth)
    ///</param>
    ///<param name="height" type="int">
    /// Height in pixels of the dialog box (default : defaultDialogHeight)
    ///</param>
    if (width == null) { width = defaultDialogWidth; }
    if (height == null) { height = defaultDialogHeight; }
    var dialogBox = $(idDiv);
    $(dialogBox).hide();
    $(dialogBox).dialog({
        title: title,
        autoOpen: false,
        resizable: false,
        modal: true,
        minHeight: height,
        minWidth: width,
        buttons: [
			{
			    text: "Ok",
			    click: function () {
			        if (redirection != null) {
			            if (redirection != "") {
			                $.fn.NavigateTo(redirection);
			            }
			        }
			        $(this).dialog("close");
			    }
			}
		],
        open: function (event, ui) { }
    });
}

$.fn.jqDialogFunction = function (divSelector, title, okFunction, cancelFunction, openFunction, width, height) {
    ///<summary>
    /// Create JQueryUI Dialog box with custom ok, cancel and open functions
    ///</summary>
    ///<param name="divSelector" type="String">
    /// The id of the div to be transformed to a dialog box
    ///</param>
    ///<param name="okFunction" type="function">
    /// Function called when Ok button clicked
    ///</param>
    ///<param name="cancelFunction" type="function">
    /// Function called when Cancel button clicked (if cancelFunction is null, dialog box is closed)
    ///</param>
    ///<param name="openFunction" type="function">
    /// Function called to initialize dialog box
    ///</param>
    ///<param name="width" type="int">
    /// Width in pixels of the dialog box (default : defaultDialogWidth)
    ///</param>
    ///<param name="height" type="int">
    /// Height in pixels of the dialog box (default : defaultDialogHeight)
    ///</param>
    if (width == null) { width = defaultDialogWidth; }
    if (height == null) { height = defaultDialogHeight; }
    var dialogBox = $(divSelector)
    if (cancelFunction == null) {
        cancelFunction = function () {
            $(this).dialog("close");
        }
    }
    $(dialogBox).hide();
    $(dialogBox).dialog({
        title: title,
        autoOpen: false,
        resizable: false,
        modal: true,
        minHeight: height,
        minWidth: width,
        open: openFunction,
        buttons: [
			{
			    text: "Ok",
			    click: okFunction
			}
            ,
            {
                text: "Cancel",
                click: cancelFunction
            }
		]
    });
    var form = dialogBox.find("form");
    if (form != null) {
        $.validator.unobtrusive.parse(form);
    }
    $(divSelector).dialog('open');
}

$.fn.jqDialogConfirm = function (title, message, confirmButtonTitle, cancelButtonTitle, okFunction, height, width) {
	/// <summary>
	/// Simple confirmation dialog box
	/// </summary>
    /// <param name="title" type="String">
    /// Title of the dialog
    /// </param>
    /// <param name="message" type="String">
    /// Message to display
    /// </param>
    /// <param name="confirmButtonTitle" type="String">
    /// Confirm button title
    /// </param>
    /// <param name="cancelButtonTitle"type="String">
    /// Cancel button title
    /// </param>
    /// <param name="okFunction" type="function">
    /// Function called when Ok button clicked
    ///</param>
    ///<param name="width" type="int">
    /// Width in pixels of the dialog box (default : defaultSmallDialogWidth)
    ///</param>
    ///<param name="height" type="int">
    /// Height in pixels of the dialog box (default : defaultSmallDialogHeight)
    ///</param>
    if (width == null) { width = defaultSmallDialogWidth; }
    if (height == null) { height = defaultSmallDialogHeight; }
    var dialogBox = $('#dialogMessage');
    dialogBox.text(message);
    $(dialogBox).hide();
    $(dialogBox).dialog({
        title: title,
        autoOpen: false,
        resizable: false,
        modal: true,
        minHeight: height,
        minWidth: width,
        buttons: [
			{
			    text: confirmButtonTitle,
			    click: okFunction
			}
            ,
            {
                text: cancelButtonTitle,
                click: function () {
                    $(this).dialog("close");
                }
            }
		]
    });
    dialogBox.dialog('open');
}

$.fn.jqDialogYesNoFunction = function (title, message, yesButtonTitle, noButtonTitle, yesFunction, noFunction, openFunction, width, height) {
    /// <summary>
    /// Yes / No JQuery UI dialog box
    /// </summary>
    /// <param name="title" type="String">
    /// Title of the dialog
    /// </param>
    /// <param name="message" type="String">
    /// Message to display
    /// </param>
    /// <param name="yesButtonTitle" type="String">
    /// Confirm button title
    /// </param>
    /// <param name="noButtonTitle"type="String">
    /// Cancel button title
    /// </param>
    /// <param name="yesFunction" type="function">
    /// Function called when yes button clicked
    /// </param>
    /// <param name="noFunction" type="function">
    /// Function called when no button clicked
    /// </param>
    /// <param name="openFunction" type="function">
    /// Function called to initialize dialog
    /// </param>
    ///<param name="width" type="int">
    /// Width in pixels of the dialog box (default : defaultSmallDialogWidth)
    ///</param>
    ///<param name="height" type="int">
    /// Height in pixels of the dialog box (default : defaultSmallDialogHeight)
    ///</param>
    if (width == null) { width = defaultSmallDialogWidth; }
    if (height == null) { height = defaultSmallDialogHeight; }
    var dialogBox = $('#dialogMessage');
    dialogBox.text(message);
    $(dialogBox).hide();
    $(dialogBox).dialog({
        title: title,
        autoOpen: false,
        resizable: false,
        modal: true,
        width: width,
        height: height,
        open: openFunction,
        closeOnEscape: false,
        buttons: [
			{
			    text: yesButtonTitle,
			    click: yesFunction
			}
            ,
            {
                text: noButtonTitle,
                click: noFunction
            }
		]
    });
    var form = dialogBox.find("form");
    if (form != null) {
        $.validator.unobtrusive.parse(form);
    }
    //$(".ui*").mouseout(function (e) { e.preventDefault()});
    dialogBox.dialog('open');
}

$.fn.jqDialogAlert = function (title, message, redirection, width, height) {
    /// <summary>
    /// Alert dialog box
    /// </summary>
    /// <param name="title" type="String">
    /// Title of the dialog
    /// </param>
    /// <param name="message" type="String">
    /// Message to display
    /// </param>
    /// <param name="redirection" type="String">
    /// Navigate to redirection address if not null
    /// </param>
    ///<param name="width" type="int">
    /// Width in pixels of the dialog box (default : defaultSmallDialogWidth)
    ///</param>
    ///<param name="height" type="int">
    /// Height in pixels of the dialog box (default : defaultSmallDialogHeight)
    ///</param>
    if (width == null) { width = defaultSmallDialogWidth; }
    if (height == null) { height = defaultSmallDialogHeight; }
    $.fn.jqDialog('#dialogMessage', title, redirection, width, height);
    $('#dialogMessage').html('');
    $('#dialogMessage').text(message);
    $('#dialogMessage').dialog('open');
}

$.fn.jqDialogAjaxError = function (title, message, redirection, width, height) {
    /// <summary>
    /// Display a dialog message completed with a IFrame to display a error message coming from server
    /// </summary>
    /// <param name="title" type="String">
    /// Title of the dialog
    /// </param>
    /// <param name="message" type="String">
    /// Message to display
    /// </param>
    /// <param name="redirection" type="String">
    /// Navigate to redirection address if not null
    /// </param>
    ///<param name="width" type="int">
    /// Width in pixels of the dialog box (default : defaultDialogWidth)
    ///</param>
    ///<param name="height" type="int">
    /// Height in pixels of the dialog box (default : defaultDialogHeight)
    ///</param>
    if (width == null) { width = defaultDialogWidth; }
    if (height == null) { height = defaultDialogHeight; }
    $.fn.jqDialog('#dialogMessage', title, redirection, width, height, false);
    var iFrame = $("#dialogMessage").find(".ui-dialog-content").find("#AjaxErrorIFrame");
    $('#dialogMessage').html('');
    if (iFrame.length == 0) {
        iFrame = $("<iframe id='AjaxErrorIFrame' name='AjaxErrorIFrame' frameborder='0' scrolling='yes' width='570' height='270'>");
        iFrameParent = $("#dialogMessage");
        iFrameParent.append(iFrame);
    }
    var doc = ($("#AjaxErrorIFrame")[0]).contentWindow;
    doc.document.open();
    doc.document.write(message);
    window.document.close();
    $('#dialogMessage').dialog('open');
}

/*#endregion*/